//
//  HZC_BaseViewController.m
//  Co8
//
//  Created by Hanzengchao on 2017/11/1.
//

#import "YYJ_BaseViewController.h"

@interface YYJ_BaseViewController ()<UIGestureRecognizerDelegate,UITableViewDelegate,UITableViewDataSource,UINavigationBarDelegate>

@property (strong ,nonatomic) UILabel*kInternetLabel;
@property (nonatomic, strong)UIView *customizedStatusBar;
@end

@implementation YYJ_BaseViewController
+(instancetype)sharedViewController{
    return [[YYJ_BaseViewController alloc] init];
}
-(UILabel *)kInternetLabel{
    if(!_kInternetLabel){
        _kInternetLabel = [[UILabel alloc]init];
        _kInternetLabel.text = NSLocalizedString(@"CMH_NoInternetConnection_Text",nil);
//        _kInternetLabel.backgroundColor = pinkColor;
//        _kInternetLabel.textColor = fullWhiteColor;
        _kInternetLabel.alpha = 1;
        _kInternetLabel.hidden = YES;
        [_kInternetLabel setFont:[UIFont systemFontOfSize:13]];
//        _kInternetLabel.frame = CGRectMake(0, 0, kScreenWidth,30);
        _kInternetLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _kInternetLabel;
}
-(void)setNaItemTransparent:(BOOL)naItemTransparent{
    _naItemTransparent = naItemTransparent;
    
//    if (@available(ios 15.0, *)) { //UINavigationBarAppearance属性从iOS13开始
//        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
//        [appearance configureWithOpaqueBackground];
//        if (self.naItemTransparent) {
//            appearance.backgroundColor = [UIColor clearColor];
//        }else{
//            appearance.backgroundColor = [UIColor whiteColor];
//        }
//    }else{
//        if (self.naItemTransparent) {
//            [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//            [self.navigationController.navigationBar setShadowImage:[UIImage new]];
//             self.navigationController.navigationBar.translucent = YES;
//            self.navBarHairlineImageView.hidden = YES;
//        }else{
//            [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
//            [self.navigationController.navigationBar setShadowImage:nil];
//            self.navigationController.navigationBar.translucent = NO;
//            self.navBarHairlineImageView.hidden = NO;
//        }
//    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    if (@available(ios 15.0, *)) { //UINavigationBarAppearance属性从iOS13开始
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        
        if (self.naItemTransparent) {
            [appearance configureWithTransparentBackground];
            appearance.backgroundColor = [UIColor clearColor];
            appearance.shadowColor = [UIColor clearColor];
            self.navigationController.navigationBar.scrollEdgeAppearance = nil;
            self.navigationController.navigationBar.translucent = YES;
        }else{
            self.navigationController.navigationBar.translucent = NO;
            [appearance configureWithOpaqueBackground];
            appearance.backgroundColor = [UIColor whiteColor];
            appearance.shadowColor = [UIColor blackColor];
            appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
            self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
        }
        if (language_is_mon) {
            appearance.titleTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"MongolianWhite" size:17],NSForegroundColorAttributeName:self.naItemColor};
            
        }else{
            appearance.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:self.naItemColor};
        }
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.tintColor = self.naItemColor;
    }else {
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        if (self.naItemTransparent) {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
            [self.navigationController.navigationBar setShadowImage:[UIImage new]];
             self.navigationController.navigationBar.translucent = YES;
            self.navBarHairlineImageView.hidden = YES;
        }else{
            self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
            [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
            [self.navigationController.navigationBar setShadowImage:nil];
            self.navigationController.navigationBar.translucent = NO;
            self.navBarHairlineImageView.hidden = NO;
        }
        if (language_is_mon) {
            [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"MongolianWhite" size:17],NSForegroundColorAttributeName:self.naItemColor}];
        }else{
            [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:self.naItemColor}];
        }
        self.navigationController.navigationBar.tintColor = self.naItemColor;
    }
    [self setStatusBarColor:[UIColor clearColor]];
//    if (@available(ios 15.0, *)) { //UINavigationBarAppearance属性从iOS13开始
//        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
//        appearance.backgroundColor = [UIColor whiteColor];
//        appearance.backgroundEffect = nil;
//    }
}
+(void)setNavcBarAppearance:(UIViewController *)vc
          naItemTransparent:(BOOL)naItemTransparent
                 titleColor:(UIColor *)naItemColor{
    if (@available(ios 15.0, *)) { //UINavigationBarAppearance属性从iOS13开始
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        [appearance configureWithOpaqueBackground];
        if (naItemTransparent) {
            [appearance configureWithTransparentBackground];
            appearance.backgroundColor = [UIColor clearColor];
            appearance.shadowColor = [UIColor clearColor];
            vc.navigationController.navigationBar.scrollEdgeAppearance = nil;
            vc.navigationController.navigationBar.translucent = YES;
        }else{
            vc.navigationController.navigationBar.translucent = NO;
            [appearance configureWithOpaqueBackground];
            appearance.backgroundColor = [UIColor whiteColor];
            appearance.shadowColor = [UIColor blackColor];
            appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
            vc.navigationController.navigationBar.scrollEdgeAppearance = appearance;
        }
        if (language_is_mon) {
            appearance.titleTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"MongolianWhite" size:17],NSForegroundColorAttributeName:naItemColor};
            
        }else{
            appearance.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:naItemColor};
        }
        vc.navigationController.navigationBar.standardAppearance = appearance;
    }else {
        vc.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        if (naItemTransparent) {
            [vc.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
            [vc.navigationController.navigationBar setShadowImage:[UIImage new]];
             vc.navigationController.navigationBar.translucent = YES;
//            vc.navBarHairlineImageView.hidden = YES;
        }else{
            vc.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
            [vc.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
            [vc.navigationController.navigationBar setShadowImage:nil];
            vc.navigationController.navigationBar.translucent = NO;
//            vc.navBarHairlineImageView.hidden = NO;
        }
        if (language_is_mon) {
            [vc.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"MongolianWhite" size:17],NSForegroundColorAttributeName:naItemColor}];
        }else{
            [vc.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:naItemColor}];
        }
        vc.navigationController.navigationBar.tintColor = naItemColor;
    }
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.
    interactivePopGestureRecognizer.delegate = self;
    switch (self.kBarColor) {
        case statusBarColor_Default:
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
            self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
            break;
        case statusBarColor_LightContent:
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
            self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
            break;
        default:
            break;
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NetworkStatusYes" object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
    [self interfaceOrientation:UIInterfaceOrientationPortrait];
    [super viewDidDisappear:animated];
    if (@available(ios 13.0, *)) {
        if (self.customizedStatusBar) {
            [self.customizedStatusBar removeFromSuperview];
            self.customizedStatusBar = nil;
        }
    }
}

//通过一个方法来找到这个黑线(findHairlineImageViewUnder):
 - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
-(UIImageView *)navBarHairlineImageView{
    if (!_navBarHairlineImageView) {
        _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    }
    return _navBarHairlineImageView;
}
-(void)setStatusBarColor:(UIColor *)color{
    if (@available(iOS 13.0, *)) {// iOS 13 不能直接获取到statusbar 手动添加个view到window上当做statusbar背景
        if (!self.customizedStatusBar) {
            //获取keyWindow
            UIWindow *keyWindow = [self getKeyWindow];
            self.customizedStatusBar = [[UIView alloc] initWithFrame:keyWindow.windowScene.statusBarManager.statusBarFrame];
            self.customizedStatusBar.backgroundColor = color;
            [keyWindow addSubview:self.customizedStatusBar];
        }
    }
    else {
        self.customizedStatusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    }

    if ([self.customizedStatusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        self.customizedStatusBar.backgroundColor = color;
    }
}
 
- (UIWindow *)getKeyWindow
{
    // 获取keywindow
    NSArray *array = [UIApplication sharedApplication].windows;
    UIWindow *window = [array objectAtIndex:0];
 
     //  判断取到的window是不是keywidow
    if (!window.hidden || window.isKeyWindow) {
        return window;
    }
 
    //  如果上面的方式取到的window 不是keywidow时  通过遍历windows取keywindow
    for (UIWindow *window in array) {
        if (!window.hidden || window.isKeyWindow) {
            return window;
        }
    }
    return nil;
}
 
//界面销毁时把添加的状态栏从window上移除
- (void)dealloc{
    if (@available(ios 13.0, *)) {
        if (self.customizedStatusBar) {
            [self.customizedStatusBar removeFromSuperview];
            self.customizedStatusBar = nil;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.view.backgroundColor) {
        self.view.backgroundColor = [UIColor whiteColor];
    }
//    self.view.backgroundColor = [UIColor whiteColor];
    self.naItemTransparent = NO;
    self.kBarColor = statusBarColor_Default;
    self.naItemColor = [UIColor blackColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NetworkStatus:) name:@"NetworkStatusYes" object:nil];
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
    keyboardManager.enable = YES; // 控制整个功能是否启用
    keyboardManager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
    keyboardManager.shouldToolbarUsesTextFieldTintColor = YES; // 控制键盘上的工具条文字颜色是否用户自定义
    keyboardManager.toolbarManageBehaviour = IQAutoToolbarBySubviews; // 有多个输入框时，可以通过点击Toolbar 上的“前一个”“后一个”按钮来实现移动到不同的输入框
    keyboardManager.enableAutoToolbar = NO; // 控制是否显示键盘上的工具条
//    keyboardManager.shouldShowTextFieldPlaceholder = YES; // 是否显示占位文字
    keyboardManager.placeholderFont = [UIFont boldSystemFontOfSize:17]; // 设置占位文字的字体
    keyboardManager.keyboardDistanceFromTextField = 10.0f; // 输入框距离键盘的距离
    keyboardManager.previousNextDisplayMode = IQPreviousNextDisplayModeAlwaysShow;
    self.kShutDownSlidingPop = YES;
    [self.view addSubview:self.kInternetLabel];
    [self _initNav];
    [self _initView];
    [self installInaternetLabel];
  
}
-(void)installInaternetLabel{
}

-(void)_initView{
    if (self.kTableView){
        self.kTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [self contentInsetAdjustmentBehavior:self.kTableView];
    }
}
-(void)_initNavigationItem{
    
}
-(void)setNoDataViewFrame:(UIView *)view{
//    self.kNoDataView.frame = CGRectMake(0, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
}
-(UIView *)kNoDataView{
    if (!_kNoDataView) {
        _kNoDataView = GetNibView(@"YYJ_NoDataView");
        _kNoDataView.backgroundColor = [UIColor clearColor];
    }
    return _kNoDataView;
}
-(UITableView *)kTableView{
    if (!_kTableView) {
        for (UIView *SubView in self.view.subviews) {
            if ([SubView isKindOfClass:[UITableView class]]) {
                _kTableView = (UITableView *)SubView;
                _kTableView.delegate = self;
                _kTableView.dataSource = self;
            }
        }
    }
    return _kTableView;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return self.kShutDownSlidingPop;
}
-(void)_initNav{
    UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc] init];
    returnButtonItem.title = @"";
    self.navigationItem.backBarButtonItem = returnButtonItem;
}
-(void)loadView{
    [super loadView];
}

#pragma mark - ios11 滚动视图的界面需要执行的方法
-(void)contentInsetAdjustmentBehavior:(UIScrollView *)ScrollView{
    if (@available(iOS 11.0, *)) {
        ScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // ignored(忽视)消除对应的selector的警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
        self.automaticallyAdjustsScrollViewInsets = NO;
#pragma clang diagnostic pop
    }
}
-(NSArray *)getInvite{
    return nil;
}
-(void)NetworkStatus:(NSNotification*)kNoc{
    [self installInaternetLabel];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotate {
    return YES;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED{
    return UIInterfaceOrientationMaskPortrait;
}
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector             = (SEL)NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val                  = orientation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

#pragma mark - tableView 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *kTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
    if (!kTableViewCell) {
        kTableViewCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellId"];
        kTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return kTableViewCell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
